package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.happyscan138.makefun.BaseAdapter.Comment_item;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.questionDetailed;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */

public class CommentAdapter extends BaseAdapter {
    private Context mContext;
    public int positionUserID;
    public  int  tag;
    public Comment_item bean;



    public void setOnItemAddClick(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    public  OnAddClickListener onItemAddClick;

    public CommentAdapter(Context mContext, List<Comment_item> listData ) {
        this.mContext = mContext;
        this.listData = listData;

    }
    public CommentAdapter() {
    }
    private List<Comment_item> listData;
    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, final ViewGroup parent) {
        final ViewHolder_Comment viewHolder;
        if(view==null){
            viewHolder=new ViewHolder_Comment();

            view=View.inflate(mContext, R.layout.comment_item,null);
            viewHolder.Comment_head=(RoundImageView) view.findViewById(R.id.comment_head);
            viewHolder.Comment_name=(TextView) view.findViewById(R.id.comment_name);
            viewHolder.Comment_time=(TextView) view.findViewById(R.id.comment_time);
            viewHolder.Comment_text=(TextView) view.findViewById(R.id.comment_text);
            viewHolder.replay=(Button)view.findViewById(R.id.replay);


            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_Comment) view.getTag();
        }
        bean=listData.get(position);
        Picasso.with(mContext).load(bean.head).into(viewHolder.Comment_head);
        viewHolder.Comment_name.setText(bean.name);
        viewHolder.Comment_time.setText(bean.time);
        viewHolder.Comment_text.setText(bean.text);
        viewHolder.replay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((questionDetailed)mContext).CommentLinearalayout.setVisibility(View.VISIBLE);
                tag=2;
                positionUserID=Integer.valueOf(bean.User_ID);
                if(onItemAddClick != null) {
                    onItemAddClick.onItemClick(positionUserID,tag);
                }
            }
        });
        return view;
    }
    public static interface OnAddClickListener {
        // true add; false cancel
        public void onItemClick(int positionID,int tag); //传递boolean类型数据给activity


    }
}
class ViewHolder_Comment{
    public RoundImageView Comment_head;
    public TextView Comment_name;
    public TextView Comment_time;
    public TextView Comment_text;
    public Button replay;

}